package com.itheima.reggie.utils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;

/**
 * 短信发送工具类
 */

@Component
public class SMSUtils {

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 邮箱验证码发送
     * @param phone
     * @param code
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    public  void sendMessage(String phone,String code) throws MessagingException, UnsupportedEncodingException {
        // 创建一个邮件消息
        MimeMessage message = javaMailSender.createMimeMessage();

        // 创建 MimeMessageHelper
        MimeMessageHelper helper = new MimeMessageHelper(message, false);

        String to = phone + "@163.com";

        // 发件人邮箱和名称
        helper.setFrom("18201680308@163.com", "瑞吉外卖");
        // 收件人邮箱
        helper.setTo(to);
        // 邮件标题
        helper.setSubject("验证码");
        // 邮件正文，第二个参数表示是否是HTML正文
        helper.setText("您的验证码为:" + code + ",请妥善保管", true);

        // 发送
        javaMailSender.send(message);

    }

}
